/* 本地存储文件 */

/** ***************** localStorage ********************* */
/**
 * 设置localStorage
 * @param {string} key
 * @param {object} value
 */
export function setLocalStorage(key, value) {
  localStorage.setItem(key, JSON.stringify(value))
}

/**
 * 获取localStorage
 * @param {string} key
 */
export function getLocalStorage(key) {
  return JSON.parse(localStorage.getItem(key))
}

/**
 * 移除localStorage
 * @param {string} key
 */
export function removeLocalStorage(key) {
  localStorage.removeItem(key)
}

/** ***************** sessionStorage ********************* */
/**
 * 设置sessionStorage
 * @param {string} key
 * @param {object} value
 */
export function setSessionStorage(key, value) {
  sessionStorage.setItem(key, JSON.stringify(value))
}

/**
 * 获取sessionStorage
 * @param {string} key
 */
export function getSessionStorage(key) {
  return JSON.parse(sessionStorage.getItem(key))
}

/**
 * 移除sessionStorage
 * @param {string} key
 */
export function removeSessionStorage(key) {
  sessionStorage.removeItem(key)
}

/** ***************** cookie ************************** */
/**
 * 获取cookie
 * @param {string} name
 */
export function getCookie(name) {
  const arr = document.cookie.match(new RegExp(`(^| )${name}=([^;]*)(;|$)`))
  if (arr != null) {
    return decodeURI(arr[2])
  }
  return null
}

/**
 * 设置cookie
 * @param {string} name
 * @param {string} value
 * @param {number} seconds
 */
export function setCookie(name, value, seconds) {
  const s = seconds || 0 // seconds有值就直接赋值，没有为0
  let expires = ""
  if (s !== 0) {
    // 设置cookie生存时间
    const date = new Date()
    date.setTime(date.getTime() + s * 1000)
    expires = `; expires=${date.toGMTString()}`
  }
  document.cookie = `${name}=${escape(value)}${expires}; path=/` // 转码并赋值
}

/**
 * 移除cookie
 * @param {string} name
 */
export function removeCookie(name) {
  let expires = ""
  const date = new Date()
  date.setTime(date.getTime() - 1)
  expires = `; expires=${date.toGMTString()}`
  document.cookie = `${name}=''${expires}; path=/` // 转码并赋值
}
